import numpy as np
import matplotlib.pyplot as plt
from python_ai.common.xcommon import sep
from python_ai.ML_2.decomposition.follow_teacher.x_pca_impl import x_pca, x_pca_by_svd
from sklearn.decomposition import TruncatedSVD, PCA
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import StandardScaler

x, y = load_breast_cancer(return_X_y=True)
x = StandardScaler().fit_transform(x)

fig = plt.figure(figsize=[8, 8])
spr = 2
spc = 2
spn = 0

title = 'by self impl'
sep(title)
spn += 1
plt.subplot(spr, spc, spn)
x_new, lmds, us = x_pca(x, 2)
print(lmds)
plt.title(title)
plt.scatter(x_new[:, 0], x_new[:, 1], c=y, s=1)
plt.show()

title = 'by PCA'
sep(title)
spn += 1
plt.subplot(spr, spc, spn)
model = PCA(n_components=2)
x_new = model.fit_transform(x)
lmds = model.explained_variance_
print(lmds)
plt.title(title)
plt.scatter(x_new[:, 0], x_new[:, 1], c=y, s=1)
plt.show()

title = 'by TruncatedSVD'
sep(title)
spn += 1
plt.subplot(spr, spc, spn)
model = TruncatedSVD(n_components=2)
x_new = model.fit_transform(x)
lmds = model.explained_variance_
print(lmds)
plt.title(title)
plt.scatter(x_new[:, 0], x_new[:, 1], c=y, s=1)
plt.show()

title = 'by self impl based on svd'
sep(title)
spn += 1
plt.subplot(spr, spc, spn)
x_new, lmds, us = x_pca_by_svd(x, 2)
print(lmds)
plt.title(title)
plt.scatter(x_new[:, 0], x_new[:, 1], c=y, s=1)
plt.show()
